<?php
//ob_start();
//session_start();
require_once("admin_functions.php");
chk_session();
/**************************************************************
File Name		: import.php
Purpose			: To import database
Author			: Susheel Deshmukh
Create Date		: 07/22/2002
Last Modified	: 07/22/2002
***************************************************************/

include_once("global_conf.php");
include_once("lib/main.php");
// set the timelimit
//set_time_limit($CONF['time_limit']);
include_once("lib/exportData.php");
$phpMyBackup=new phpMyBackup;
//$phpMyBackup->printHeader();
?>
<Script Language="JavaScript">
<!--

function showprompt(targeturl)
{
	if(confirm("Are you sure you want to delete the data"))
	{
		self.location.href=targeturl;
	}
	return;
}

function popUp(str)
{
	window.open(str,"tinyWindow",'toolbar=no,left=200,width=300,height=300');
}
function getFileName()
{
	var file=prompt('Please enter File Name:',' ');
	if(file==null)
	{
		return false;
	}
	else
	{
		document.frm.fileName.value=file;
		return true;
	}
}
//-->
</Script>
<html>
<head>
<title>DB Utility</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="../../include/style.css" type="text/css">
<link rel="stylesheet" href="../style.css" type="text/css">
</head>

<body bgcolor="#FFFFFF" text="#000000">
	<tr><td align=left>
		<?php include "menu.php"; ?></TD>
			<tc>
				<td valign="top" class='bodytextb'>

<table  width='80%' border="0" cellspacing="2" cellpadding="0" >
	<tr>
		<td width='25%' class='subheadinglink' align='center' valign='middle'>
			<a href='exportData.php'><font class='subheadinglink'>Export</font></a>
		</td>
		<td width='25%' class='subheadinglink' align='center' valign='middle'>
			<a href='importData.php'><font class='subheadinglink'>Import</font></a>
		</td>
		<td width='25%' class='subheadinglink' align='center' valign='middle'>
			&nbsp;
		</td>
		<td width='25%' class='subheadinglink' align='center' valign='middle'>
			&nbsp;
		</td>
	</tr>
</table>
<?php
if(!empty($mesg))
{
	print "<font class='mesg'>$mesg</font>";
}
else
{
	print "<font class='mesg'>&nbsp;&nbsp;</font>";
}
?>
<center>
		   <?php

		   				if(isset($HTTP_GET_VARS['file']) && isset($HTTP_GET_VARS['db'])) {
							//$file = trim(str_replace(".sql","",$file));
							$file = trim(str_replace(".sql.gz","",$file));
							$phpMyBackup->sqlConnect($CONF['sql_host'],$CONF['sql_usr'],$CONF['sql_pass']);
						    $db=mysql_select_db($HTTP_GET_VARS['db']);
						    $theInfoFile=file("export/".$file.".info");
						    $theInfoFileArr=explode("|", $theInfoFile[0]);
						    $totalBackupSize=$theInfoFileArr[2];
							// if the backup is gziped, uncrompress it
						    if($theInfoFileArr[5]==1) {
						        $zd = gzopen ("export/".$file.".sql.gz", "r");
						        $theFileCont = gzread ($zd, $totalBackupSize);
						        gzclose($zd);
						        $theFile=explode("\n",$theFileCont);
						    } else {
						        // this will do if it's not gziped :)
						        $theFile=file("export/".$file.".sql");
						    }

						    $table_q=array();
						    $data_q=array();
						    $type="NONE";
						    $tables_q=0;
						    foreach($theFile as $line_in_file){
						        $line_in_file=chop($line_in_file);
						        if ($type=="NONE") {
						            if(strtolower(substr($line_in_file,0,6))=="insert") {
						                $data_q[]=substr($line_in_file,0,strlen($line_in_file)-1);
						            } elseif(strtolower(substr($line_in_file,0,6))=="create") {
						                $type="TABLE";
						                $table_q[$tables_q]=$line_in_file."\n";
						            }
						        }elseif ($type=="TABLE") {
						            if(strtolower(substr($line_in_file,0,1))==")") {
						                $type="NONE";
						                $table_q[$tables_q] .= $line_in_file."\n";
						                $tables_q++;
						            } else {
						                $table_q[$tables_q] .= $line_in_file."\n";
						            }
						        }
						    }

						    $sql_error=0;
						    $count = 0;
						    foreach($table_q as $q_data) {
						        $q=mysql_query($q_data);
						        if($q != 0)
						        {
						            $count = $count + 1;
						            //print $q;
						        }

						    }
						    foreach($data_q as $q_data) {
						        $q=mysql_query($q_data);
						        if($q != 0){
						            //print $q;

						        }
						        else
						        {
						        	$count += 1;
						        }
						    }
							//print $count;
						    if($sql_error==1) {
						        echo "<p align='left'><span class='mesg'>$M_importDatabaseError";
						        echo mysql_error()."</span><br><br>";
						        echo"<table width='500' align='left'><tr><td>";
						        echo"<center><form><input type='button' name='back' value='Back' onClick='javascript:window.history.back();'></form>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</center>";
						        echo"</tr></td></table>";
						    } else {
						        echo"<p align='left'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class='mesg'>$M_databaseImportSuccessful";
						        echo"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Imported <b>".sizeof($table_q)."</b> tables, and <b>".sizeof($data_q)."</b> fields.<br><br></span>";
						        echo"<table width='400' align='left'><tr><td>";
						        echo"<center><form><input type='button' name='back1' value='Back' onClick='javascript:window.history.back();'></form></center>";
						        echo"</tr></td></table>";
						    }
						}

						@mysql_close($con);
						//$phpMyBackup->printFooter();
?>
</body>
</html>
